package com.ktgj.service.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ktgj.service.domain.County;
import com.ktgj.service.mapper.CityMapper;
import com.ktgj.service.service.CItyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.Map;

@Service
public class CItyServiceImpl implements CItyService {

    @Autowired
    private CityMapper cityMapper;

    /**
     * 更新dic_county表数据
     * @param cityDomain
     * @return
     */
    @Override
    public int insertCounty(County cityDomain) {
        int i = 0;
            String path = CItyServiceImpl.class.getClassLoader().getResource("XZQ.json").getPath();
            String s = readJsonFile(path);
            JSONObject jobj = JSON.parseObject(s);
            JSONObject address1 = jobj.getJSONObject("86");

            Map maps = (Map)JSON.parse(String.valueOf(address1));
            for (Object map : maps.entrySet()){
                System.out.println("省、直辖市：------->>>"+((Map.Entry)map).getKey()+"     " + ((Map.Entry)map).getValue());

                System.out.println("==========="+((Map.Entry)map).getValue()+"下的直辖市或者区=========");
                JSONObject jsonObject1 = jobj.getJSONObject((String) ((Map.Entry) map).getKey());
                Map maps1 = (Map)JSON.parse(String.valueOf(jsonObject1));
                for (Object map1 : maps1.entrySet()){
                    System.out.println(((Map.Entry)map1).getKey()+"     " + ((Map.Entry)map1).getValue());

                    JSONObject jsonObject2 = jobj.getJSONObject((String) ((Map.Entry) map1).getKey());
                    Map maps2 = (Map)JSON.parse(String.valueOf(jsonObject2));
                    if (maps2 != null) {
                        System.out.println("==========="+((Map.Entry)map1).getValue()+"下的市、县、区=========");
                        for (Object map2 : maps2.entrySet()){
                            cityDomain.setId((String) ((Map.Entry)map2).getKey());
                            cityDomain.setCity_id((String) ((Map.Entry)map1).getKey());
                            cityDomain.setCounty_name(((Map.Entry)map2).getValue().toString());
                            i = cityMapper.insertCounty(cityDomain);
                            System.out.println(((Map.Entry)map2).getKey()+"     " + ((Map.Entry)map2).getValue());
                        }
                    }
                }
            }
        return i;
    }

    @Override
    public int insertCity(com.ktgj.service.domain.City city) {
        int i = 0;
        String path = CItyServiceImpl.class.getClassLoader().getResource("XZQ.json").getPath();
        String s = readJsonFile(path);
        JSONObject jobj = JSON.parseObject(s);
        JSONObject address1 = jobj.getJSONObject("86");

        Map maps = (Map)JSON.parse(String.valueOf(address1));
        for (Object map : maps.entrySet()){
            System.out.println("省、直辖市：------->>>"+((Map.Entry)map).getKey()+"     " + ((Map.Entry)map).getValue());

            System.out.println("==========="+((Map.Entry)map).getValue()+"下的直辖市或者区=========");
            JSONObject jsonObject1 = jobj.getJSONObject((String) ((Map.Entry) map).getKey());
            Map maps1 = (Map)JSON.parse(String.valueOf(jsonObject1));
            for (Object map1 : maps1.entrySet()){
                city.setId((String) ((Map.Entry)map1).getKey());
                city.setProvince_id((String) ((Map.Entry)map).getKey());
                city.setCity_name(((Map.Entry)map1).getValue().toString());
                i = cityMapper.insertCity(city);
                System.out.println(((Map.Entry)map1).getKey()+"     " + ((Map.Entry)map1).getValue());
            }
        }
        return i;
    }

    /**
     * 读取json文件，返回json串
     * @param fileName
     * @return
     */
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);

            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
